<html>
<head><meta charset="utf-8"><title>exercising alignment · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html">exercising alignment</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="169226637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169226637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169226637">(Jun 28 2019 at 13:57)</a>:</h4>
<p>I have a type that <em>probably</em> needs to specify <code>#[repr(align(…))]</code>, based on a bug report I got. This type is inside another type. How can I write some code that pushes this type around in memory to attempt to reproduce the error?</p>



<a name="169226735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169226735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169226735">(Jun 28 2019 at 13:59)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">struct</span> <span class="nc">XxCore</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">v1</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">v2</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">v3</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">v4</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">XxHash64</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">total_len</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">seed</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">core</span>: <span class="nc">XxCore</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">buffer</span>: <span class="nc">Buffer</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Buffer</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">data</span>: <span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="n">CHUNK_SIZE</span><span class="p">],</span><span class="w"></span>
<span class="w">    </span><span class="n">len</span>: <span class="kt">usize</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="169226758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169226758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169226758">(Jun 28 2019 at 13:59)</a>:</h4>
<p>Namely, I’m pretty sure that <code>Buffer</code> needs to be aligned for a <code>u64</code></p>



<a name="169226835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169226835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169226835">(Jun 28 2019 at 14:00)</a>:</h4>
<p>on a 32-bit machine, the <code>usize</code> is a <code>u32</code> and I don’t get it “for free”</p>



<a name="169226995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169226995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169226995">(Jun 28 2019 at 14:02)</a>:</h4>
<p>When testing similar things, I’ve just allocated a big buffer of u8 and added byte-by-byte offsets. Can I do a similar thing  here? Except I need to control where a field is inside of another type, and <code>XxHash64</code> is presumably already aligned to a bigger value.</p>



<a name="169229150"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169229150" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169229150">(Jun 28 2019 at 14:26)</a>:</h4>
<p>I don't understand what you are trying to do. Do you want to test the alignment by casting the pointer to int at run-time and doing <code>% 8</code>? What are "byte-by-byte offsets"?</p>
<p>You <em>can</em> run it in Miri though, it will check alignment on every access... ;)</p>



<a name="169229833"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169229833" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169229833">(Jun 28 2019 at 14:36)</a>:</h4>
<p>the problem is that I treat <code>Buffer::data</code> as a <code>&amp;[u64]</code> and assume that there will never be unaligned data at the beginning. This <em>should</em> be fine once I add <code>repr[align(…)]</code> to <code>Buffer</code>, however it hasn’t tripped up any of my test assertions yet.</p>



<a name="169229873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169229873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169229873">(Jun 28 2019 at 14:36)</a>:</h4>
<p>I want to write a test</p>



<a name="169229953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169229953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169229953">(Jun 28 2019 at 14:37)</a>:</h4>
<p>I have <a href="https://github.com/shepmaster/twox-hash/blob/master/comparison/src/lib.rs#L59-L66" target="_blank" title="https://github.com/shepmaster/twox-hash/blob/master/comparison/src/lib.rs#L59-L66">similar tests for <code>&amp;[u8]</code></a>, where I make up a random <code>Vec&lt;u8&gt;</code> and a <code>usize</code> and index into it, producing a variety of starting offsets</p>



<a name="169230086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169230086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169230086">(Jun 28 2019 at 14:38)</a>:</h4>
<p>My “ideal” case would be to somehow perturbate the layout created for <code>XxHash</code> so that the <code>buffer</code> field sometimes is placed on a non-64-bit point.</p>



<a name="169230318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169230318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169230318">(Jun 28 2019 at 14:41)</a>:</h4>
<p>I think my practical workaround will be to make a <code>Vec&lt;u8&gt;</code> that is much larger than <code>Buffer</code>,  then <code>ptr::write</code> a Buffer into the vec at multiples of <code>align_of::Buffer</code>, ensuring my debug assertions don’t get tripped</p>



<a name="169230971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169230971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169230971">(Jun 28 2019 at 14:48)</a>:</h4>
<p><code>#[repr(Rust)]</code> will reorder fields to ensure that there is as little padding left inside as possible</p>



<a name="169231041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169231041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169231041">(Jun 28 2019 at 14:49)</a>:</h4>
<p>but the alignment of XxCore will still stay 64-bits in this case regardless, so you cannot just place it on non-64-bit boundary</p>



<a name="169231153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169231153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169231153">(Jun 28 2019 at 14:50)</a>:</h4>
<p>/me decides they don’t get what the intention here is either</p>



<a name="169231404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169231404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169231404">(Jun 28 2019 at 14:53)</a>:</h4>
<p>I apologize for my poor description. This seems “obvious” to me so I’m having trouble figuring how to say it differently</p>



<a name="169231478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169231478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169231478">(Jun 28 2019 at 14:54)</a>:</h4>
<p><a href="https://github.com/shepmaster/twox-hash/blob/master/src/sixty_four.rs#L188-L191" target="_blank" title="https://github.com/shepmaster/twox-hash/blob/master/src/sixty_four.rs#L188-L191">This assertion</a> is being hit by a user of my crate who is running on 32-bit x86</p>



<a name="169231545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169231545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169231545">(Jun 28 2019 at 14:55)</a>:</h4>
<p>which makes perfect sense: the <code>Buffer</code> struct will be aligned to 32-bit (due to the <code>usize</code>), which means it <em>might</em> not be aligned to 64-bit</p>



<a name="169231623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169231623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169231623">(Jun 28 2019 at 14:56)</a>:</h4>
<p>I want to write a test that will fail (cause the assertion to be triggered)</p>



<a name="169231810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169231810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169231810">(Jun 28 2019 at 14:58)</a>:</h4>
<p>So that I can see the assertion failure and then see it go away when I add the right <code>repr(align)</code></p>



<a name="169231881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169231881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169231881">(Jun 28 2019 at 14:59)</a>:</h4>
<p>My current attempt is something like</p>
<div class="codehilite"><pre><span></span><span class="w">    </span><span class="cp">#[test]</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">x</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="p">{</span><span class="n">mem</span><span class="p">,</span><span class="w"> </span><span class="n">ptr</span><span class="p">};</span><span class="w"></span>
<span class="w">        </span><span class="k">use</span><span class="w"> </span><span class="k">super</span>::<span class="p">{</span><span class="n">Buffer</span><span class="p">,</span><span class="w"> </span><span class="n">CHUNK_SIZE</span><span class="p">};</span><span class="w"></span>


<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">buffer_size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">Buffer</span><span class="o">&gt;</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">buffer_align</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mem</span>::<span class="n">align_of</span>::<span class="o">&lt;</span><span class="n">Buffer</span><span class="o">&gt;</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="c1">// Make it big enough that we don&#39;t need to worry</span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">scratch</span>: <span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">vec</span><span class="o">!</span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">buffer_size</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">10</span><span class="p">];</span><span class="w"></span>

<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">alignment_steps</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buffer_size</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="n">buffer_align</span><span class="p">;</span><span class="w"></span>

<span class="w">        </span><span class="n">std</span>::<span class="n">dbg</span><span class="o">!</span><span class="p">(</span><span class="n">buffer_size</span><span class="p">,</span><span class="w"> </span><span class="n">buffer_align</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="k">for</span><span class="w"> </span><span class="n">step</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="p">..</span><span class="o">=</span><span class="n">alignment_steps</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="c1">// Buffer must be plain data, no `Drop` implementation</span>
<span class="w">            </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">start</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">scratch</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">().</span><span class="n">add</span><span class="p">(</span><span class="n">step</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">buffer_align</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">start</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">start</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">Buffer</span><span class="p">;</span><span class="w"></span>
<span class="w">                </span><span class="n">ptr</span>::<span class="n">write</span><span class="p">(</span><span class="n">start</span><span class="p">,</span><span class="w"> </span><span class="n">Buffer</span>::<span class="n">default</span><span class="p">());</span><span class="w"></span>

<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="n">start</span><span class="p">;</span><span class="w"></span>
<span class="w">                </span><span class="n">buf</span><span class="p">.</span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">CHUNK_SIZE</span><span class="p">;</span><span class="w"></span>

<span class="w">                </span><span class="c1">// Exercise assertions in method</span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buf</span><span class="p">.</span><span class="n">as_u64_arrays</span><span class="p">();</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="n">panic</span><span class="o">!</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</pre></div>



<a name="169235871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169235871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169235871">(Jun 28 2019 at 15:44)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> the only thing you can do is to compare <code>align_of</code> .</p>



<a name="169235875"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169235875" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169235875">(Jun 28 2019 at 15:44)</a>:</h4>
<p>I think.</p>



<a name="169235993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169235993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169235993">(Jun 28 2019 at 15:46)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> you mean <code>assert_eq(align_of::&lt;u64&gt;(), align_of::&lt;Buffer&gt;())</code>?</p>



<a name="169236041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169236041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169236041">(Jun 28 2019 at 15:47)</a>:</h4>
<p>Yeah.</p>



<a name="169236089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169236089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169236089">(Jun 28 2019 at 15:47)</a>:</h4>
<p>fwiw you need your <code>align(64)</code> to be on <code>Buffer::data</code>, not the <code>Buffer</code> itself</p>



<a name="169236177"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169236177" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169236177">(Jun 28 2019 at 15:48)</a>:</h4>
<p>you can put it on members? All the docs imply it needs to go on types</p>



<a name="169236212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169236212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169236212">(Jun 28 2019 at 15:48)</a>:</h4>
<p>You’ll need to wrap your array into another struct.</p>



<a name="169236233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169236233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169236233">(Jun 28 2019 at 15:49)</a>:</h4>
<p><code>align(64)</code> or <code>align(8)</code>? I haven’t yet found a definitive unit listed anywhere</p>



<a name="169236238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169236238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169236238">(Jun 28 2019 at 15:49)</a>:</h4>
<p>don’t remember either.</p>



<a name="169236277"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169236277" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169236277">(Jun 28 2019 at 15:49)</a>:</h4>
<p>if you have multiple fields in your struct, those fields can still be re-ordered, reducing the effective alignment of your array to a lower value</p>



<a name="169236364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169236364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169236364">(Jun 28 2019 at 15:50)</a>:</h4>
<p>Another good test you can do is to assert that your field has offset that matches your alignment requirements</p>



<a name="169236797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169236797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169236797">(Jun 28 2019 at 15:55)</a>:</h4>
<p>The trouble I’m having with the last one is that ultimately I can’t test the offset effectively because the compiler can do whatever it wants</p>



<a name="169236923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169236923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169236923">(Jun 28 2019 at 15:56)</a>:</h4>
<p>For example, in the “real” case, there’s a huge stack of types: <code>hashbrown::HashMap&lt;String, InnerStmt, BuildHasherDefault&lt;XxHash64&gt;&gt;</code></p>



<a name="169236979"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169236979" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169236979">(Jun 28 2019 at 15:57)</a>:</h4>
<p>And I think that’s the one and only use of it in the program, so the compiler can do whatever fun optimization it wants to</p>



<a name="169239345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169239345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169239345">(Jun 28 2019 at 16:20)</a>:</h4>
<p>trying to rephrase what you are doing with your "testing at offsets", you basically have a closure <code>fn test_me(x: &amp;mut Buffer)</code> that you want to call with <code>Buffer</code> being as unaligned as possible?</p>



<a name="169239427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169239427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169239427">(Jun 28 2019 at 16:21)</a>:</h4>
<p>or maybe more like <code>fn test_me(place_for_buf: *mut Buffer)</code> because you need to initialize yourself or so</p>



<a name="169239576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169239576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169239576">(Jun 28 2019 at 16:23)</a>:</h4>
<p>that sounds reasonable, if you get a memory area of size <code>size_of&lt;Buffer&gt;() + 32</code> and then try all offsets less than <code>32</code> such that that you actually get a sufficiently aligned pointer, you should cover all your cases</p>



<a name="169239590"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169239590" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169239590">(Jun 28 2019 at 16:23)</a>:</h4>
<p>why would that not find such a problem? of course this only works if you can control allocation</p>



<a name="169255649"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169255649" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169255649">(Jun 28 2019 at 19:37)</a>:</h4>
<p>Yes, I think it’s accurate to say “I want to test placing my <code>Buffer</code> at every alignment it is valid for”, but as <span class="user-mention" data-user-id="123586">@nagisa</span> points out, that’s not even enough.</p>



<a name="169255729"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169255729" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169255729">(Jun 28 2019 at 19:38)</a>:</h4>
<p>Because what I really need to ensure is that a specific <em>field</em> of my <code>Buffer</code> will always be aligned correctly; I don’t care about the alignment of <code>Buffer</code> itself.</p>



<a name="169255810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169255810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169255810">(Jun 28 2019 at 19:39)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> yeah, that involves just checking that <code>align_of::&lt;Buffer&gt;() &gt;= whatyouwant</code> and <code>field_offset!(Buffer, data) % WHATYOUWANT == 0</code>.</p>



<a name="169255829"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169255829" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169255829">(Jun 28 2019 at 19:40)</a>:</h4>
<p>The problem is that there’s no test I can write that says “for every possible way that <code>repr(Rust)</code> <em>might</em> organize this struct”</p>



<a name="169255831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169255831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169255831">(Jun 28 2019 at 19:40)</a>:</h4>
<p>(<code>field_offset!</code> is not a built-in, you’ll need to figure out the incantation yourself or find a crate that does this)</p>



<a name="169255900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169255900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169255900">(Jun 28 2019 at 19:40)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> this is where <span class="user-mention" data-user-id="120791">@RalfJ</span> tells me that no implementation of <code>offsetof</code> is non-UB because we can’t get raw pointers to fields without making references <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="169255902"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169255902" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169255902">(Jun 28 2019 at 19:40)</a>:</h4>
<p>well, yeah, you cannot do <em>that</em>.</p>



<a name="169255978"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169255978" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169255978">(Jun 28 2019 at 19:41)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> well… I don’t see how <code>let x = Buffer { ... }; let fieldptr = &amp;x.data as *const _</code> is gonna be bad.</p>



<a name="169256048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169256048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169256048">(Jun 28 2019 at 19:42)</a>:</h4>
<p>that is, you don’t <em>really</em> need <code>offsetof</code>, you just need to get an offset of a field in some instantiation of type, which should be fine.</p>



<a name="169256080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169256080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169256080">(Jun 28 2019 at 19:42)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> which means I cannot directly write a test that triggers the same problem as my user; I can only test indirect things. At this point, wrapping it in a <code>#[repr(align(8)] struct AlignToU64&lt;T&gt;(T)</code> is equally as descriptive as this second-class of test.</p>



<a name="169256198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169256198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169256198">(Jun 28 2019 at 19:44)</a>:</h4>
<p>That’s what really annoys me here; I cannot reproduce their bug, even though I have really strong suspicions.</p>



<a name="169257830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169257830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169257830">(Jun 28 2019 at 20:03)</a>:</h4>
<blockquote>
<p>The problem is that there’s no test I can write that says “for every possible way that <code>repr(Rust)</code> <em>might</em> organize this struct”</p>
</blockquote>
<p>yeah that you cannot do :/</p>



<a name="169257850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169257850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169257850">(Jun 28 2019 at 20:03)</a>:</h4>
<p>and Miri cannot, either</p>



<a name="169257942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169257942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169257942">(Jun 28 2019 at 20:04)</a>:</h4>
<p>though given that your user ran into this, it seems like Rust actually <em>does</em> lay out out to your disadvantage, so running miri with <code>--target &lt;32bit target&gt;</code> should run into the problem?</p>



<a name="169258149"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169258149" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169258149">(Jun 28 2019 at 20:07)</a>:</h4>
<p>(We wanted to introduce randomization of field ordering as an option IIRC to weed out assumptions about order fields are laid out)</p>



<a name="169258284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169258284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169258284">(Jun 28 2019 at 20:09)</a>:</h4>
<p>Is it likely that Linux and Windows would lay out a struct differently (both 32-bit x86 and the same version of the compiler)</p>



<a name="169258393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169258393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169258393">(Jun 28 2019 at 20:10)</a>:</h4>
<p>They see this in multiple x86 Windows builds, but I cant see it in Linux</p>



<a name="169259695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169259695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169259695">(Jun 28 2019 at 20:30)</a>:</h4>
<p>It is possible, Windows' ABI mandates different alignments and stuff for various things.</p>



<a name="169259748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169259748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169259748">(Jun 28 2019 at 20:31)</a>:</h4>
<p>ah dang... cross-miri is a thing on my list but currently it is hard due to <a href="https://github.com/rust-lang/rust/issues/56443" target="_blank" title="https://github.com/rust-lang/rust/issues/56443">https://github.com/rust-lang/rust/issues/56443</a></p>



<a name="169259799"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169259799" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169259799">(Jun 28 2019 at 20:32)</a>:</h4>
<p>in principle miri is entirely cross-capable, there's just some silly build system stuff in the way</p>



<a name="169259802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169259802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169259802">(Jun 28 2019 at 20:32)</a>:</h4>
<p>for the libstd</p>



<a name="169259809"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169259809" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169259809">(Jun 28 2019 at 20:32)</a>:</h4>
<p>it insists on building an artifact we dont want nor need, and needs a target toolchain just for that :(</p>



<a name="169296837"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/exercising%20alignment/near/169296837" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/exercising.20alignment.html#169296837">(Jun 29 2019 at 13:13)</a>:</h4>
<p>For those interested, I <em>can</em> reproduce, but so far only by running on a 32-bit Windows machine, running their full test suite which requires MySQL</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>